<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\AuditFlowModel;
use cmf\controller\AdminBaseController;
use tree\Tree;
use think\Db;

class AuditFlowController extends AdminBaseController
{

    /**
     * 审核流程列表
     * @adminMenu(
     *     'name'   => '审核流程管理',
     *     'parent' => 'admin/AuditFlow/index',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 40,
     *     'icon'   => '',
     *     'remark' => '审核流程管理',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $auditFlowModel = new AuditFlowModel();

        $lists          = $auditFlowModel
                       -> alias('a')
                       -> field('a.*,r.name as role_name')
                       -> join('__ROLE__ r','a.role_id=r.id')
                       -> order('audit_id desc')
                       -> select();
                       // pre($lists,1);
        $this->assign('lists', $lists);

        $actions = ['add', 'edit', 'delete', 'toggleStatus'];
        $this->assign('actions', actions_check($this->request->module().'/'.$this->request->controller().'/', $actions));

        return $this->fetch();
    }

    /**
     * 添加审核流程
     * @adminMenu(
     *     'name'   => '添加审核流程',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加审核流程',
     *     'param'  => ''
     * )
     */
    public function add()
    {

        return $this->fetch();
    }

    /**
     * 添加审核流程提交
     * @adminMenu(
     *     'name'   => '添加审核流程提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加审核流程提交',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {
        $data   = $this->request->param();
        $result = $this->validate($data, 'AuditFlow');
        if ($result !== true) {
            $this->error($result);
        }

        $user_id = cmf_get_current_admin_id();
        if( $user_id==1 ){
            $role_id=1;
        }else{
            $role_id = db('role_user')->where('user_id',$user_id)->value('role_id');
        }
        $data['role_id'] = $role_id;
        
        
        $auditFlowModel = new AuditFlowModel();
        $result         = $auditFlowModel->validate(true)->save($data);

        if ($result === false) {
            $this->error($auditFlowModel->getError());
        }

        //操作日志
        admin_log($data['name'], 'add', 'audit_flow');

        $this->success("添加成功！", url("audit_flow/index"));
    }

    /**
     * 编辑审核流程
     * @adminMenu(
     *     'name'   => '编辑审核流程',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑审核流程',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
        $id             = $this->request->param('id');
        $auditFlowModel = new AuditFlowModel();
        $result         = $auditFlowModel->where('audit_id', $id)->find();
        $this->assign('result', $result);
        return $this->fetch();
    }

    /**
     * 编辑审核流程提交
     * @adminMenu(
     *     'name'   => '编辑审核流程提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑审核流程提交',
     *     'param'  => ''
     * )
     */
    public function editPost()
    {
        $data   = $this->request->param();
        $result = $this->validate($data, 'AuditFlow');
        if ($result !== true) {
            $this->error($result);
        }

        $auditFlowModel = new AuditFlowModel();
        $result         = $auditFlowModel->validate(true)->save($data, ['audit_id' => $data['audit_id']]);
        if ($result === false) {
            $this->error($auditFlowModel->getError());
        }

        //操作日志
        admin_log($data['name'], 'edit', 'audit_flow');

        $this->success("保存成功！", url("audit_flow/index"));
    }

    /**
     * 删除审核流程
     * @adminMenu(
     *     'name'   => '删除审核流程',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除审核流程',
     *     'param'  => ''
     * )
     */
    public function delete()
    {
        $id             = $this->request->param('id', 0, 'intval');
        $auditFlowModel = new AuditFlowModel();
        $data           = $auditFlowModel->where('audit_id', $id)->find();
        $result         = $auditFlowModel->where('audit_id', $id)->delete();

        if ( $result!=false ){
            //操作日志
            admin_log($data['name'], 'delete', 'audit_flow');

            $this->success("删除成功！");
        }else{
            $this->error("删除失败");
        }
    }

    public function toggleStatus()
    {
        $id             = $this->request->param('id', 0, 'intval');
        $auditFlowModel = new AuditFlowModel();
        $status         = $auditFlowModel->where('audit_id', $id)->value('status');

        $data['status'] = $status==1 ? 0 : 1;

        $result         = $auditFlowModel->where('audit_id', $id)->update($data);

        if ( $result!=false ){
            //操作日志
            admin_log($auditFlowModel->value('name'), 'edit', 'log_audit_flow_status');

            $this->success("操作成功");
        }else{
            $this->error("操作失败");
        }   
    }
}
